package com.deyuanyun.pic.pipe.service.impl;

import com.deyuanyun.pic.common.dto.DTOResult;
import com.deyuanyun.pic.common.exception.ExceptionCode;
import com.deyuanyun.pic.common.exception.ExceptionCodeUtil;
import com.deyuanyun.pic.common.exception.bizcode.basedata.pipe.PipeSteelPipeCodeEnum;
import com.deyuanyun.pic.common.exception.code.ModuleMsg;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.pipe.mapper.dao.SteelPipeDao;
import com.deyuanyun.pic.pipe.service.PipeSteelPipeService;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class PipeSteelPipeServiceImpl implements PipeSteelPipeService {

	@Resource
	private SteelPipeDao steelMapper;

	@Override
	public DTOResult getSteelBaseByParam(String pipeLineId,
										 String pipeSegmentId, BigDecimal startMileage,
										 BigDecimal endMileage, Integer pageNum, Integer pageSize) {

		int page = 0;

		int size = 0;
		// 初始化当前页数
		if (pageNum == null || "".equals(pageNum)) {
			page = 0;
		} else {
			page = pageNum;
		}
		// 初始化每页显示数量
		if (pageSize == null || "".equals(pageSize)) {
			size = 20;
		} else {
			size = pageSize;
		}
		PageHelper.startPage(page, size);

		if (null == pipeLineId || "".equals(pipeLineId)) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeSteelPipeCodeEnum.pipeLineIdIsNull.getCode(),
					"管线没有选择");
		}

		if (null == pipeSegmentId || "".equals(pipeSegmentId)) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeSteelPipeCodeEnum.pipeSegmentIdIsNull
									.getCode(),
					"管段没有选择");
		}

		// 定义一个里程起止点
		BigDecimal mileageStart = new BigDecimal(0);

		// 定义一个里程终点
		BigDecimal mileageEnd = new BigDecimal(0);

		// 如果传入的起止点不为NULL或 ""
		if (startMileage != null) {
			mileageStart = startMileage;
		}

		// 如果传入的终点不为NULL或""
		if (endMileage != null && "".equals(endMileage)) {
			mileageEnd = endMileage;
		}

		// 定义一个管段ID
		String pipeSegmentIdStr = "";

		if (!"all".equals(pipeSegmentId)) {
			pipeSegmentIdStr = pipeSegmentId;
		}

		Page<HashMap<String, Object>> map = null;

		// 在DAO层获取一个MAP集合对象
		map = steelMapper.selectStellBaseByParam(pipeLineId, pipeSegmentIdStr,
				mileageStart, mileageEnd);

		if (map == null) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeSteelPipeCodeEnum.mapperIsNull.getCode(),
					PipeSteelPipeCodeEnum.mapperIsNull.getLable());
		}

		if (map.size() <= 0) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeSteelPipeCodeEnum.mapperIsNull.getCode(),
					PipeSteelPipeCodeEnum.mapperIsNull.getLable());
		}

		return new DTOResult(ExceptionCode.SUCCESS.getCode(),
				ExceptionCode.SUCCESS.getLabel(), map);
	}

	@Override
	public List<List<String[]>> getExcel(String pipeLineId,
			String pipeSegmentId, BigDecimal startMileage, BigDecimal endMileage) {
		// TODO Auto-generated method stub

		// 定义一个返回值
		List<List<String[]>> result = new ArrayList<List<String[]>>();

		// 定义一个里程起止点
		BigDecimal mileageStart = new BigDecimal(0);

		// 定义一个里程终点
		BigDecimal mileageEnd = new BigDecimal(0);

		// 如果传入的起止点不为NULL或 ""
		if (startMileage != null) {
			mileageStart = startMileage;
		}

		// 如果传入的终点不为NULL或""
		if (endMileage != null && "".equals(endMileage)) {
			mileageEnd = endMileage;
		}

		// 定义一个管段ID
		String pipeSegmentIdStr = "";

		if (!"all".equals(pipeSegmentId)) {
			pipeSegmentIdStr = pipeSegmentId;
		}

		List<LinkedHashMap<String, Object>> l = steelMapper
				.selectDownLoadToExcel(pipeLineId, pipeSegmentIdStr,
						mileageStart, mileageEnd);

		// 把一个List<LinkedHashMap<String, Object>> 转换成 List<String[]>
				ArrayList<LinkedHashMap<String, Object>> list2= new ArrayList<LinkedHashMap<String, Object>>();
				
				for (int i = 0; i < l.size(); i++) {
				LinkedHashMap<String, Object> map = l.get(i);
					map.put("PIPE_TYPE", CodeUtil.getName((String) map.get("PIPE_TYPE")));
					map.put("MATERIAL", CodeUtil.getName((String) map.get("MATERIAL")));
					map.put("MANUPIPESEAM_TYPE", CodeUtil.getName((String) map.get("MANUPIPESEAM_TYPE")));
//					map.put("DIAMETER", CodeUtil.getName((String) map.get("DIAMETER")));
					map.put("COATING_MATERIAL", CodeUtil.getName((String) map.get("COATING_MATERIAL")));
					map.put("COATING_SOURCE", CodeUtil.getName((String) map.get("COATING_SOURCE")));
					map.put("INTER_NALOREXTERNAL", CodeUtil.getName((String) map.get("INTER_NALOREXTERNAL")));
					list2.add(map);
				}
		List<String[]> lsr = JXL.toList(list2, "yyyy-MM-dd HH:mm:ss");

		result.add(lsr);

		return result;

	}

	@Override
	public Page<HashMap<String, Object>> getDetails(String pipeLineId,
			String pipeSegmentId, BigDecimal startMileage,
			BigDecimal endMileage, Integer pageNum, Integer pageSize) {

		int page = 1;

		int size = 10;
		// 初始化当前页数
		if (pageNum != null) {
			page = pageNum;
		}
		// 初始化每页显示数量
		if (pageSize != null) {
			size = pageSize;
		}
		PageHelper.startPage(page, size);

		// 定义一个里程起止点
		BigDecimal mileageStart = new BigDecimal(0);

		// 定义一个里程终点
		BigDecimal mileageEnd = new BigDecimal(0);

		// 如果传入的起止点不为NULL或 ""
		if (startMileage != null) {
			mileageStart = startMileage;
		}

		// 如果传入的终点不为NULL或""
		if (endMileage != null &&!"".equals(endMileage)) {
			mileageEnd = endMileage;
		}
		Page<HashMap<String, Object>> l=steelMapper.getPipeSteelDetails(pipeLineId, pipeSegmentId,
		mileageStart, mileageEnd);
		Page<HashMap<String, Object>> list2= new Page<HashMap<String, Object>>();
		for (int i = 0; i < l.size(); i++) {
		HashMap<String, Object> map =l.get(i);
			map.put("PIPE_TYPE", CodeUtil.getName((String) map.get("PIPE_TYPE")));
			map.put("MATERIAL", CodeUtil.getName((String) map.get("MATERIAL")));
			map.put("MANUPIPESEAM_TYPE", CodeUtil.getName((String) map.get("MANUPIPESEAM_TYPE")));
//			map.put("DIAMETER", CodeUtil.getName((String) map.get("DIAMETER")));
			map.put("COATING_MATERIAL", CodeUtil.getName((String) map.get("COATING_MATERIAL")));
			map.put("COATING_SOURCE", CodeUtil.getName((String) map.get("COATING_SOURCE")));
			map.put("INTER_NALOREXTERNAL", CodeUtil.getName((String) map.get("INTER_NALOREXTERNAL")));
			list2.add(map);
		}
		list2.setTotal(l.getTotal());
		list2.setPageNum(l.getPageNum());
		list2.setPageSize(l.getPageSize());
		return list2;
	}

	/*
	 * @Override public DTOResult getDetails(String pipeLineId, String
	 * pipeSegmentId, BigDecimal startMileage, BigDecimal endMileage, Integer
	 * pageNum, Integer pageSize) { int page = 0;
	 * 
	 * int size = 0; // 初始化当前页数 if (pageNum == null || "".equals(pageNum)) {
	 * page = 0; } else { page = pageNum; } // 初始化每页显示数量 if (pageSize == null ||
	 * "".equals(pageSize)) { size = 20; } else { size = pageSize; }
	 * PageHelper.startPage(page, size);
	 * 
	 * if (null == pipeLineId || "".equals(pipeLineId)) { return new DTOResult(
	 * ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA) +
	 * PipeSteelPipeCodeEnum.pipeLineIdIsNull.getCode(), "管线没有选择"); }
	 * 
	 * if (null == pipeSegmentId || "".equals(pipeSegmentId)) { return new
	 * DTOResult( ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA) +
	 * PipeSteelPipeCodeEnum.pipeSegmentIdIsNull .getCode(), "管段没有选择"); }
	 * 
	 * // 定义一个里程起止点 BigDecimal mileageStart = new BigDecimal(0);
	 * 
	 * // 定义一个里程终点 BigDecimal mileageEnd = new BigDecimal(0);
	 * 
	 * // 如果传入的起止点不为NULL或 "" if (startMileage != null) { mileageStart =
	 * startMileage; }
	 * 
	 * // 如果传入的终点不为NULL或"" if (mileageEnd != null && "".equals(mileageEnd)) {
	 * mileageEnd = endMileage; }
	 * 
	 * // 定义一个管段ID String pipeSegmentIdStr = "";
	 * 
	 * if (!"all".equals(pipeSegmentId)) { pipeSegmentIdStr = pipeSegmentId; }
	 * 
	 * Page<HashMap<String, Object>> map = null;
	 * 
	 * map=steelMapper.getPipeSteelDetails(pipeLineId, pipeSegmentIdStr,
	 * mileageStart, mileageEnd);
	 * 
	 * return new DTOResult(ExceptionCode.SUCCESS.getCode(),
	 * ExceptionCode.SUCCESS.getLabel(), map); }
	 */

}
